﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Department.App_Code;

namespace Department
{
    public partial class DepartmentMaster : System.Web.UI.MasterPage
    {

        string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;
        static SqlConnection con;
        public Department.App_Code.Department Department { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    Convert.ToInt32(Request.Params["ID"]); //Validating URL parameters
                }
                catch (Exception) // Catch OverflowException and FormatException
                {
                    Response.Redirect("/Default.aspx?id=" + PageProperties.GetMainPageID());
                }

                Department = DepartmentProperties.GetDepartment(Request);

                //HomeLink.NavigateUrl = "~/Default.aspx?id=" + PageProperties.GetMainPageID();

                //HeaderText.Text = getHeaderText();
                con = new SqlConnection(conStr);


                //Populating the menu
                DataSet ds = new DataSet();
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    string sql = "SELECT ID, Name, ParentID, @Dept as Dept, @Lang as Lang from Menu WHERE IsHidden = 0 AND FacDeptID = @FacDep ORDER BY Position";
                    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                    da.SelectCommand.Parameters.AddWithValue("@FacDep", Department.ID);
                    da.SelectCommand.Parameters.AddWithValue("@Dept", Request["dept"]);
                    da.SelectCommand.Parameters.AddWithValue("@Lang", Request["lang"]);
                    da.Fill(ds);
                    da.Dispose();
                }
                ds.DataSetName = "Menus";
                ds.Tables[0].TableName = "Menu";
                DataRelation relation = new DataRelation("ParentChild",
                        ds.Tables["Menu"].Columns["ID"],
                        ds.Tables["Menu"].Columns["ParentID"], true);

                relation.Nested = true;
                ds.Relations.Add(relation);

                xmlDataSource.Data = ds.GetXml();
            }
        }

        protected void menu_MenuItemClick(object sender, MenuEventArgs e)
        {
            this.UpdateMainPanel.Update();
        }

        protected void ButtonSearch_Click(object sender, ImageClickEventArgs e)
        {
            string parameter = TextBoxSearch.Text;
            Response.Redirect("/Search.aspx?q=" + parameter);
        }

    }

}
